package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Aygitihbar;
import hibernate.model.Musteriaygitlari;
import hibernate.service.EmmyService;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("AygitihbarBean")
@Scope("session")
public class AygitihbarBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;	
	
	private Aygitihbar selectedAygitihbar;
	private List<Aygitihbar> aygitihbarList;
	
	private transient MusteriBean musBean;
	
	private String today;
	private String bitisTarihi;
	private String kayipCbitisTarihi;
	
	@Override
	@PostConstruct
	public void init() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Calendar cal = Calendar.getInstance();
		today = sdf.format(cal.getTime());
		//aygitihbarList = (List<Aygitihbar>) emmyService.findAll(new Aygitihbar());
	}

	@Override
	public String restart() {
		init();
		return "";
	}

	@Override
	public String deleteView() {
		emmyService.delete(selectedAygitihbar);
		init();
		return "list";
	}

	@Override
	public String viewNew() {		
		MusteriAygitlariBean musayBn = (MusteriAygitlariBean)emmyService.getBean(new MusteriAygitlariBean());
		
		if(musayBn.getSelectedMusAygit() !=null){
			if(checkihbar(musayBn.getSelectedMusAygit())){
				musayBn.setIslemTuru("ihbar");
				selectedAygitihbar= new Aygitihbar();
				selectedAygitihbar.setMusteriaygitlari(musayBn.getSelectedMusAygit());
				musBean = (MusteriBean) emmyService.getBean(new MusteriBean());
			
				selectedAygitihbar.setMusteri(musBean.getCurrentMusteri());
				selectedAygitihbar.setIhbarTarihi(today);
			}
			else{
				musayBn.setSelectedMusAygit(null);
				FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Uyarı !!!", 
						"Ürünle ilgili ihbarda bulunulmuş");
				FacesContext.getCurrentInstance().addMessage(null, msg);
			}
		}else{
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Hata!!!", "Ürün şeçilmedi");
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
		return "";
	}

	@Override
	public String viewDetail() { /*
		List<Aygitihbar> valueList = (List<Aygitihbar>) aygitihbarTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = aygitihbarTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentAygitihbar= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentAygitihbar.getIhbarid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		} */ return "detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException {
		System.out.println("Aygıt ihbar save");
		System.out.println("tür : "+selectedAygitihbar.getIhbarTuru());
		if(emmyService.saveOrUpdate(selectedAygitihbar)){
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Process Completed..."); 
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
		else{
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "unsuccessful", 
					"İşlem Başarısız, Tekrar deneyiniz."); 
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
		clear();
		return "";
	}
	
	public boolean checkihbar(Musteriaygitlari musAgtBean){
		List<?> lt =emmyService.getQuery("findAygitihbSrn", "aSrn", musAgtBean.getAygitSeriNo());
		System.out.println("size : "+lt.size());
		if(lt.size()>0 ){
			return false;
		}
		return true;
	}
	
	public void clear() {
		selectedAygitihbar = null;
		( (MusteriAygitlariBean)emmyService.getBean(new MusteriAygitlariBean()) ).clear();
	}
	
	@SuppressWarnings("unchecked")
	public void getMusteriIhbarlari() {
		System.out.println("musteri ihbarları");
		try{
			musBean = (MusteriBean) emmyService.getBean(new MusteriBean());
			aygitihbarList = (List<Aygitihbar>) emmyService.getQuery
				("findAygitihbKlc", "aKlc", musBean.getCurrentMusteri().getKullaniciid());
			System.out.println("mus ibh size : "+aygitihbarList.size());
		}catch(java.lang.NullPointerException npe){
			System.out.println("aygıt ihbar : getMusteriIhbarlari NPE : "+npe.getMessage());
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "unsuccessful", 
					"İşlem Başarısız veya login hatası, Lütfen tekrar deneyiniz."); 
			FacesContext.getCurrentInstance().addMessage(null, msg);
		}
	}
	
	@SuppressWarnings("unchecked")
	public String getDetayliAygitihbar(){		
		aygitihbarList = (List<Aygitihbar>) emmyService.getQuery(createQuery());
		return "";
	}
	
	@Override
	public String createQuery(){
		Aygitihbar agyihb = selectedAygitihbar;
		String query = "from Aygitihbar";	
		
		if(agyihb == null){
			return query;
		}
		if(agyihb.getMusteri().getKullaniciid() != 0){
			if(query.indexOf("where") != -1){
				query += " and kullaniciid="+agyihb.getMusteri().getKullaniciid()+"";
				
			}
			else{
				query += " where kullaniciid="+agyihb.getMusteri().getKullaniciid()+"";			
			}
		}
		if(agyihb.getMusteriaygitlari().getAygitSeriNo() != 0){
			if(query.indexOf("where") != -1){
				query += " and aygitSeriNo="+agyihb.getMusteriaygitlari().getAygitSeriNo()+"";
				
			}
			else{
				query += " where aygitSeriNo="+agyihb.getMusteriaygitlari().getAygitSeriNo()+"";			
			}
		}
		if(agyihb.getIhbarTuru() != null){
			if(query.indexOf("where") != -1){
				query += " and ihbarTuru like '%"+agyihb.getIhbarTuru()+"%'";
				
			}
			else{
				query += " where ihbarTuru like '%"+agyihb.getIhbarTuru()+"%'";			
			}
		}
		if(agyihb.getAciklamalar() != null){
			if(query.indexOf("where") != -1){
				query += " and aciklamalar like '%"+agyihb.getAciklamalar()+"%'";
				
			}
			else{
				query += " where aciklamalar like '%"+agyihb.getAciklamalar()+"%'";			
			}
		}
		if(agyihb.getIhbarTarihi() != null){
			if(query.indexOf("where") != -1){
				query += " and (ihbarTarihi between"+agyihb.getIhbarTarihi()+" and "+bitisTarihi+")";
				
			}
			else{
				query += " where (ihbarTarihi between"+agyihb.getIhbarTarihi()+" and "+bitisTarihi+")";			
			}
		}
		if(agyihb.getKayipCalintiTarihi() != null){
			if(query.indexOf("where") != -1){
				query += " and (kayipCalintiTarihi between"+agyihb.getKayipCalintiTarihi()+" and "+kayipCbitisTarihi+")";
				
			}
			else{
				query += " where (kayipCalintiTarihi between"+agyihb.getKayipCalintiTarihi()+" and "+kayipCbitisTarihi+")";			
			}
		}
		return query;		
	}	
	
	@Override
	public void getSession() {
		// TODO Auto-generated method stub
		
	}

	public List<Aygitihbar> getAygitihbarList() {
		return aygitihbarList;
	}

	public void setAygitihbarList(List<Aygitihbar> aygitihbarList) {
		this.aygitihbarList = aygitihbarList;
	}

	public Aygitihbar getSelectedAygitihbar() {
		return selectedAygitihbar;
	}

	public void setSelectedAygitihbar(Aygitihbar selectedAygitihbar) {
		this.selectedAygitihbar = selectedAygitihbar;
	}

	public String getBitisTarihi() {
		return bitisTarihi;
	}

	public void setBitisTarihi(String bitisTarihi) {
		this.bitisTarihi = bitisTarihi;
	}

	public String getKayipCbitisTarihi() {
		return kayipCbitisTarihi;
	}

	public void setKayipCbitisTarihi(String kayipCbitisTarihi) {
		this.kayipCbitisTarihi = kayipCbitisTarihi;
	}

	public MusteriBean getMusBean() {
		return musBean;
	}

	public void setMusBean(MusteriBean musBean) {
		this.musBean = musBean;
	}

	public String getToday() {
		return today;
	}

	public void setToday(String today) {
		this.today = today;
	}
}